package com.innova_t.android.mario.items;


import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.innova_t.android.mario.ItemsDbAdapter;
import com.innova_t.android.mario.R;

public class ContactDetailActivity extends ListActivity {

	private static final String TAG = "ListohItem";
	private ItemsDbAdapter mDbHelper;
	public static final String REST_URL = "URL";
	
	private String urlRest;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        mDbHelper = new ItemsDbAdapter(this);
        mDbHelper.open();
        
        fillData();
        
        Log.i(TAG, "data rellenada "  );
        
        urlRest = savedInstanceState != null ? savedInstanceState.getString(REST_URL) 
				: null;
		if (urlRest == null) {
			Bundle extras = getIntent().getExtras();
			urlRest = extras != null ? extras.getString(REST_URL)
					: null;
		}
        
		
		Log.i(TAG, "client inicializado" );
    }
	
	private void fillData() {
        Cursor itemsCursor = mDbHelper.fetchAllItems();
        startManagingCursor(itemsCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{ItemsDbAdapter.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter items = new SimpleCursorAdapter(this,
				R.layout.notes_row, itemsCursor, from, to);
        
        setListAdapter(items);
    }
	
	@Override
    protected void onResume() {
        super.onResume();
        fillData();
    }
	
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        mDbHelper.checkoutItem(id);
        
        fillData();
        
        SimpleCursorAdapter current = (SimpleCursorAdapter) l.getAdapter();
        current.notifyDataSetInvalidated();
        
        Cursor item = mDbHelper.fetchItems(id);
        
        String itid = item.getString(
	            item.getColumnIndexOrThrow(ItemsDbAdapter.KEY_ITEM));
        
        String pid = item.getString(
	            item.getColumnIndexOrThrow(ItemsDbAdapter.KEY_PARENT));
        
//        ListItemDTO listoItem = new ListItemDTO();
//        listoItem.setId( Long.parseLong(itid)  );
//        listoItem.setParentSimpleListEntityKey( Long.parseLong(pid)  );
        
        Log.i(TAG, "url a llamar " + urlRest );
		
        // initialize the connection
//        ClientResource cr = 
//        		new ClientResource("http://beta.list-oh.com/rest" + urlRest);
//		
//        // due to a gae bug
//        cr.setRequestEntityBuffering(true);
//		
//        clientRes = 
//        		(ListoItemClientResource) cr.wrap(ListoItemClientResource.class);
//		
//		
//        
//        Log.i(TAG, "llamaos al id: " + listoItem.getId() +
//        		" and pid:" + listoItem.getParentSimpleListEntityKey() );
//        
//		clientRes.store(listoItem );
//        

    }
	
}
